﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SysManage_Others_ListName : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        if (!IsPostBack)
        { Bind(Session["UserCode"].ToString()); }
    }

    #region 初始化绑定列表数据
    protected void Bind(string UserCode)
    {
        // 页面权限
        comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "ListName" },
            Grv, new string[] { "cmdAdd", "cmdEdit" }, new int[] { Grv.Columns.Count - 2, Grv.Columns.Count - 1 }, CommonModel.loginOut2);
        QueryBind(0); TypeBind();
    }
    #endregion

    protected void TypeBind()
    {
        ddlType.DataSource = help.GetDataTable("select	RC.ColumnType,RC.ColumnName from	B_Roles_Column RC where DeleteFlag<>1 and DropDownListNameFlag=1");
        ddlType.DataValueField = "ColumnType";
        ddlType.DataTextField = "ColumnName";
        ddlType.DataBind();
        ddlType.Items.Insert(0, new ListItem("请选择(Please Select)", "-1"));
    }

    #region 换页
    protected void Grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            Grv.PageIndex = e.NewPageIndex;
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }
        catch
        { }
    }
    #endregion

    //Gridview 行事件
    protected void Grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //鼠标经过事件
        if (e.Row.RowIndex != -1)
        {
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#CCCC99';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;");
                ((LinkButton)e.Row.Cells[Grv.Columns.Count - 1].Controls[0]).Attributes.Add("onclick", "return confirm('确实要删除？');");
            }
        }
    }

    #region 查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        QueryBind(1);
        HidQueryFlag.Value = "1";
    }
    //
    protected void QueryBind(int n)
    {
        string StrSql = " select B_ListNameID, ListNameType,ListNameTypeName, ListName, ListDesc "
            + " from B_ListName LN where LN.DeleteFlag<>1";

        if (n == 1)
        {
            if (!string.IsNullOrEmpty(txtName.Text.ToString()))
            {
                StrSql += string.Format(" and LN.ListName like '%{0}%'", txtName.Text);
            }
            if (!string.IsNullOrEmpty(txtDesc.Text.ToString()))
            {
                StrSql += string.Format(" and LN.ListDesc like '%{0}%'", txtDesc.Text);
            }
            if (!ddlType.SelectedValue.Equals("-1"))
            {
                StrSql += string.Format(" and LN.ListNameType='{0}'", ddlType.SelectedValue.ToString());
            }
        }

        DataTable Dt = help.GetDataTable(StrSql);
        Grv.DataSource = Dt;
        Grv.DataBind();

        if (Dt.Rows.Count > 0)
        {
            // 设置分页行永远显示
            Grv.BottomPagerRow.Visible = true;
            //赋值Label记录条数
            Label label1 = Grv.BottomPagerRow.FindControl("lblCount") as Label;
            label1.Text = Dt.Rows.Count.ToString();
        }
    }
    #endregion

    #region 添加
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.loginOut2);
        
        //获取当前用户名称
        string UserName = Session["UserCode"].ToString();
        //当前时间
        string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");

        string StrInsert = string.Format("Insert Into B_ListName(ListNameType, ListName, ListDesc,DeleteFlag,AddAuthor,"
            + "AddDate,UpdateAuthor,UpdateDate,ListNameTypeName) Values('{0}','{1}','{2}',0,'{3}','{4}','{5}','{6}','{7}')",
            ddlType.SelectedValue, txtName.Text.ToString(), txtDesc.Text.ToString(), UserName, StrDate, UserName, StrDate,
            ddlType.SelectedItem.Text);

        if (help.RunSqlInt(StrInsert) > 0)
        {
            CommonMethod.regAlertMessage("操作成功-Success!", this);
            QueryBind(0);
            HidQueryFlag.Value = "0";

            ddlType.Focus();
        }
        else
        {
            CommonMethod.regAlertMessage("Failed!", this);
            ddlType.Focus();
        }

    }
    #endregion

    #region 编辑
    protected void cmdEdit_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl2);
        string StrUpdate = string.Format(" Update B_ListName set ListNameType='{0}',ListName='{1}', ListDesc='{2}'"
            + ",UpdateAuthor='{3}',UpdateDate='{4}',ListNameTypeName='{5}' where B_ListNameID={6}",
            ddlType.SelectedValue.ToString(), txtName.Text, txtDesc.Text,
            System.DateTime.Now.ToString("yyyy-MM-dd hh:ss"), Session["UserCode"].ToString(),
            ddlType.SelectedItem.Text, hidEdit.Value);
        if (help.RunSqlInt(StrUpdate) > 0)
        {
            CommonMethod.regAlertMessage("Success！", this);

            QueryBind(0);
            HidQueryFlag.Value = "0";

            cmdAdd.Enabled = true;
            cmdEdit.Enabled = false;

            txtDesc.Text = "";
            txtName.Text = "";
        }
        else
        { CommonMethod.regAlertMessage("Failed！", this); }
    }
    #endregion

    #region GridView 行事件
    //行删除
    protected void Grv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.loginOut2);

        string strID = Grv.DataKeys[e.RowIndex]["B_ListNameID"].ToString();

        string StrDelete = " Delete from B_ListName where B_ListNameID='" + strID + "'";
        if (help.RunSqlInt(StrDelete) > 0)
        {
            CommonMethod.regAlertMessage("删除成功！", this);
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }
        else { CommonMethod.regAlertMessage("Failed!", this); }
    }
    //行编辑
    protected void Grv_SelectedIndexChanged(object sender, EventArgs e)
    {
        hidEdit.Value = Grv.DataKeys[Grv.SelectedIndex]["B_ListNameID"].ToString();
        ddlType.SelectedValue = Grv.DataKeys[Grv.SelectedIndex]["ListNameType"].ToString();
        txtName.Text = Grv.Rows[Grv.SelectedIndex].Cells[2].Text.Trim().ToString();
        txtDesc.Text = Grv.DataKeys[Grv.SelectedIndex]["ListDesc"].ToString();

        Grv.Rows[Grv.SelectedIndex].Enabled = false;
        cmdAdd.Enabled = false;
        cmdEdit.Enabled = true;
    }
    #endregion
    //重置
    protected void cmdClear_ServerClick(object sender, EventArgs e)
    {
        cmdAdd.Enabled = true;
        cmdEdit.Enabled = false;
        txtDesc.Text = "";
        txtName.Text = "";
        ddlType.SelectedIndex = 0;
    }

    //跳页
    protected void Grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "NewPage")
            {
                TextBox tb = Grv.BottomPagerRow.FindControl("txtNewPageIndex") as TextBox;
                int pageindex = Convert.ToInt32(tb.Text);
                Grv.PageIndex = pageindex - 1;
                //数据绑定
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
        }
        catch
        {
            CommonMethod.regAlertMessage("输入不合法-Error format!", this);
        }
    }
}
